<?php
/**
 * Minhlog 基类控制器
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Minh
 * @package    Minh_Controller
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: Action.php 37 2010-05-10 16:20:47Z Minh.Shu $
 *
 */
class Minh_Controller_Action extends Zend_Controller_Action
{

    /**
     * Minh_Controller_Action单例
     * @var Minh_Controller_Action
     */
    private static $_instance = null;

    /**
     * 获取单例方法
     * @return $_instance
     */
    public static function getInstance ()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * 初始化方法
     * @return void
     */
    public function init ()
    {
        //获取缓存
        Minh_Cache_File::setCacheStatus(CACHE_OPTION);
        $this->cache = Minh_Cache_File::getCache();
        //获取视图
        $this->view = Zend_Registry::get('view');
        //获取SEO信息
        $this->seo = Zend_Registry::get('seo');
        //设置跳转方式
        $this->_redirect = $this->_helper->getHelper('Redirector');
        $this->_redirect->setCode(301)->setExit(true);
    }

    /**
     * 获取执行时间
     * @return void
     */
    protected function fetchExecuteTime ()
    {
        $this->view->executeTime = number_format(microtime(true) - START_TIME, 8, '.', ''); //页面执行时间
    }

    /**
     * 获取用户ip方法
     * @return mixed $ip
     */
    protected function getIP ()
    {
        if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
            $ip = getenv("HTTP_CLIENT_IP");
        else
            if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
                $ip = getenv("HTTP_X_FORWARDED_FOR");
            else
                if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
                    $ip = getenv("REMOTE_ADDR");
                else
                    if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
                        $ip = $_SERVER['REMOTE_ADDR'];
                    else
                        $ip = "unknown";
        return $ip;
    }
}